package com.guozcc.gateway.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class FileUtils {

    /**
     * inputStream ===> ByteArrayOutputStream
     */
    public static ByteArrayOutputStream inputStream2ByteArrayOutputStream(InputStream in) throws Exception{
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = in.read(b))!=-1){
            bos.write(b,0,len);
        }
        return bos;
    }
    /**
     * inputStream ===> byte[]
     */
    public static byte[] inputStream2bytes(InputStream in) throws Exception{
        ByteArrayOutputStream bos = inputStream2ByteArrayOutputStream(in);
        return bos.toByteArray();
    }
    /**
     * inputStream ===> String
     */
    public static String inputStream2String(InputStream in,String encoding) throws Exception{
        ByteArrayOutputStream bos = inputStream2ByteArrayOutputStream(in);
        return new String(bos.toByteArray(),encoding);
    }
}
